import 'package:flutter/material.dart';
import '../../widgets/NavigationView.dart';
import '../../services/umeng_analytics.dart';
import '../../services/push_service.dart';
import 'package:cached_network_image/cached_network_image.dart'; // 添加导入

class ProfilePage extends StatefulWidget {
  const ProfilePage({super.key});

  @override
  State<ProfilePage> createState() => _ProfilePageState();
}

class _ProfilePageState extends State<ProfilePage> {
  bool _notificationsEnabled = true;

  @override
  void initState() {
    super.initState();
    UmengAnalytics.onPageStart('ProfilePage');
  }

  @override
  void dispose() {
    UmengAnalytics.onPageEnd('ProfilePage');
    super.dispose();
  }

  void _toggleNotifications(bool value) {
    setState(() {
      _notificationsEnabled = value;
    });

    if (value) {
      PushService.setAlias('user_12345');
    } else {
      PushService.removeAlias('user_12345');
    }

    UmengAnalytics.onEvent(
      'toggle_notifications',
      properties: {'enabled': value},
    );
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: const NavigationView(title: '我的'),
      body: ListView(
        children: [
          const SizedBox(height: 20),
          const CircleAvatar(
            radius: 50,
            backgroundImage: CachedNetworkImageProvider(
              'https://picsum.photos/200',
            ),
          ),
          const SizedBox(height: 20),
          const Center(
            child: Text(
              '用户名',
              style: TextStyle(fontSize: 24, fontWeight: FontWeight.bold),
            ),
          ),
          const SizedBox(height: 30),
          SwitchListTile(
            title: const Text('推送通知'),
            subtitle: const Text('接收新闻推送通知'),
            value: _notificationsEnabled,
            onChanged: _toggleNotifications,
          ),
          const Divider(),
          ListTile(
            leading: const Icon(Icons.history),
            title: const Text('浏览历史'),
            onTap: () {
              // 跳转到浏览历史页面
            },
          ),
          const Divider(),
          ListTile(
            leading: const Icon(Icons.bookmark),
            title: const Text('我的收藏'),
            onTap: () {
              // 跳转到收藏页面
            },
          ),
          const Divider(),
          ListTile(
            leading: const Icon(Icons.settings),
            title: const Text('设置'),
            onTap: () {
              // 跳转到设置页面
            },
          ),
          const Divider(),
          ListTile(
            leading: const Icon(Icons.info),
            title: const Text('关于我们'),
            onTap: () {
              // 跳转到关于页面
            },
          ),
        ],
      ),
    );
  }
}
